package com.ihr360.oneclick.controller;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.ihr360.commons.entity.AuthUserInfo;
import com.ihr360.oneclick.request.SearchRequest;
import com.ihr360.oneclick.request.query.StaffRecordQueryParam;
import com.ihr360.oneclick.service.ExcelHandlerService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * excel 下载
 *
 * @author tom
 * @date 18-1-9
 */
@Controller
@RequestMapping("/excel/download")
public class ExcelDownloadController {

    @Autowired
    private ExcelHandlerService excelHandlerService;

    @GetMapping(value= "/template", produces= ("text/html;charset=UTF-8"))
    public void template(@RequestParam String termId, @RequestParam String templateType, HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        String fileName = new String(("oneClickTemplate.xls").getBytes("UTF-8"), "iso-8859-1");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        excelHandlerService.excelImportTemplateDownLoad("one.click.payroll", templateType, response.getOutputStream());
    }

    @GetMapping(value = "/data", produces = ("text/html;charset=UTF-8"))
    public void data(@RequestParam String spreadsheetId, @RequestParam(required = false) List<String> idList,
                     HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        String fileName = new String(("oneClickData.xls").getBytes("UTF-8"), "iso-8859-1");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        excelHandlerService.spreadsheetDataToExcelFile(spreadsheetId, idList, response.getOutputStream());
    }

    @GetMapping(value = "/staff/record", produces = ("text/html;charset=UTF-8"))
    public void staffRecordExport(@RequestParam(required = false) List<String> idList, @RequestParam(required = false) String searchRequest,
                                  HttpServletResponse response, AuthUserInfo authUserInfo) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        String fileName = new String(("staffRecord.xls").getBytes("UTF-8"), "iso-8859-1");
        response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
        SearchRequest<StaffRecordQueryParam> request = null;
        if (StringUtils.isNotBlank(searchRequest)) {
            request = JSONObject.parseObject(searchRequest, new TypeReference<SearchRequest<StaffRecordQueryParam>>() {
            });
        }
        excelHandlerService.staffRecordDataToExcelFile(request, idList, response.getOutputStream(), authUserInfo.getCompanyId());
    }

}
